package cate.game.robot;

import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.client.msg.MergeMsgSender;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import cate.game.role.fight.FuncPosition;
import cate.game.role.fight.HeroGrid;
import cate.game.role.fight.Position;
import cp.solution.business.robot.cmd.RobotCreateMode;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@Slf4j
public class RobotEditor {

    public static void edit(Role role, GameBody game, int mode) {
        if(RobotCreateMode.CLASSIC.id == mode) {
            editClassic(role, game);
        } else if(RobotCreateMode.DWS.id == mode) {
            editDws(role, game);
        }
    }

    private static void editClassic(Role role, GameBody game) {
        role.getBase().level = 10 + (int)(Math.random() * 150);
        long m1 = (long)(Math.random() * 30000);
        role.getBase().addVipPoint(m1);
        role.getBag().numRes.addM1(m1);
    }

    private static void editDws(Role role, GameBody game) {
        role.getBase().level = 50 + (int)(Math.random() * 150);
        long m1 = (long)(Math.random() * 10000);
        role.getBase().addVipPoint(m1);
        role.getBag().numRes.addM1(m1);

        MergeMsgSender sender = new MergeMsgSender();
        // 钱包
        game.table.item.numRes.list.forEach(row -> {
            long num = row.getPileMax();//10000000l
            role.getBag().numRes.addItem(row.id, num, sender);
        });
        //道具
        game.table.item.base.list.forEach(row -> {
            if (row.gmProducible) {
                long num = row.getPileMax();//10000000l
                if (num > 100) {
                    num *= 0.01;
                }
                role.getBag().prop.addItem(row.id, num, sender);
            }
        });

        addHeros(role, game, 5 + (int)(Math.random() * 5));
    }

    private static void addHeros(Role role, GameBody game, int star) {
        MergeMsgSender sender = new MergeMsgSender();
        game.table.hero.base.list.forEach(
                tpl -> {
                    if (tpl.real && tpl.gmProducible) {
                        if (tpl.star == star) {
                            role.getBag().hero.forGmAddCapacity();
                            role.getBag().hero.addItem(tpl.id, 1, sender);
                        }
                    }
                });

        role.getBag().hero.getList().forEach(h -> {
            int lvTimes = (int)(Math.random() * 20);
            for(int i = 0; i < lvTimes; i++) {
                if(h.level >= 30 && h.level % 10 == 0) {
                    game.train.hero.gradeUp(role, h.uid);
                }
                game.train.hero.levelUp(role, h.uid);
            }
        });

        setPos(role, game, GDFunc.ADVENTURE);
        setPos(role, game, GDFunc.DWS);
    }

    private static void setPos(Role role, GameBody game, int funcId) {
        FuncPosition pos = new FuncPosition();
        pos.funcId = funcId;
        pos.positions = new ArrayList<>();
        Set<Integer> protoSet = new HashSet<>();
        for(byte pn = 1; pn <= 3; pn++) {
            Position p = new Position();
            p.tid = 1;//弦月阵	2#4#7#8#9#10
            p.posNum = pn;
            p.grids = new ArrayList<>();
            byte[] POSES = new byte[] { 2,4,7,8,9,10 };
            for (byte n = 1; n <= 5; n++) {
                try {
                    HeroGrid g = makeHeroGrid(protoSet, game, role, n, POSES[n]);
                    if(g != null) {
                        p.grids.add(g);
                    }
                } catch (Exception e) {
                    break;
                }
            }
            pos.positions.add(p);
        }
        EcResult r = game.fight.position.updatePosition(role, pos);
        if(!r.ok()) {
            log.warn("设置布阵异常 {}", r.message);
        }
        role.getBase().updatePower();
    }

    private static HeroGrid makeHeroGrid(Set<Integer> protoSet, GameBody game, Role role, int i, byte pos) {
        HeroGrid hg = new HeroGrid();
        hg.pos = pos;
        final int GAP = 6;
        int index = GAP*(i-1) + (int)(GAP*Math.random());
        if(index >= role.getBag().hero.getList().size()) {
            index = i;
        }
        Hero hero = role.getBag().hero.getList().get(index);
        int pid = hero.getTpl(game).protoId;
        if(protoSet.contains(pid)) {
            return null;
        }
        protoSet.add(pid);
        hg.uid = hero.uid;
        return hg;
    }
}
